package com.lsu.command.experiment;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 具体的命令实现
 *
 * @Author wang suo
 * @Date 2020/10/21 0021 14:39
 * @Version 1.0
 */
public class ConcreteCommand implements Command {

    private Receiver receiver;
    private List<Circle> circles;
    private ConcreteInvoker concreteInvoker;

    ConcreteCommand(Receiver receiver, ConcreteInvoker concreteInvoker) {
        this.receiver = receiver;
        this.circles = new ArrayList<>();
        this.concreteInvoker = concreteInvoker;
    }

    @Override
    public void execute(JPanel jPanel, Circle circle) {
        receiver.addSpot(jPanel, circle);
        circles.add(circle);
    }

    @Override
    public void undo(JPanel jPanel) {
        Circle circle;
        int size = circles.size();
        if (size >= 1) {
            circle = circles.get(size - 1);
            receiver.deleteSpot(jPanel, circle);
            circles.remove(size - 1);
            concreteInvoker.notifyObservers(2, circle.getX(), circle.getY());
        }
    }
}
